package org.lifei.auth.service.impl;

import org.lifei.auth.domain.Permission;
import org.lifei.auth.service.IPermissionScanService;
import org.lifei.auth.service.IPermissionService;
import org.lifei.base.annotation.LifPermission;
import org.lifei.base.enums.RequestTypeEnum;
import org.lifei.base.util.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;
@Service
public class PermissioncScanServiceImpl implements IPermissionScanService {
    @Value("${scan.package-name}")
    private String packages;
    @Autowired
    private IPermissionService permissionService;

    @Override
    public void scan() throws ClassNotFoundException {
        permissionService.deleteAll();
        //获取所有的COntroller
        List<Class> allClassName = ClassUtils.getAllClassName(packages);
        for (Class clazz : allClassName){
            //判断是否有权限注解
            LifPermission annotation = (LifPermission)clazz.getAnnotation(LifPermission.class);
            if (Objects.isNull(annotation)){
                continue;
            }
            RequestMapping annotation2 = (RequestMapping)clazz.getAnnotation(RequestMapping.class);
            String classUrl = annotation2.value()[0];
            Permission parentPermisson = new Permission();
            parentPermisson.setSn(clazz.getSimpleName());
            parentPermisson.setParent(null);
            parentPermisson.setUrl(classUrl);
            parentPermisson.setDescs(annotation.desc());
            parentPermisson.setName(annotation.name());
            permissionService.insert(parentPermisson);
            //获取所有的方法
            Method[] methods = clazz.getMethods();
            for (Method m: methods){
                //判断方法上是否有权限注解
                LifPermission annotation1 = m.getAnnotation(LifPermission.class);
                if (Objects.isNull(annotation1)){
                    continue;
                }
                Permission permission = new Permission();
                permission.setName(annotation1.name());
                permission.setDescs(annotation1.desc());
                permission.setParent(parentPermisson);
                //SN是唯一标识：COntroller+方法名
                permission.setSn(clazz.getSimpleName()+":"+m.getName());
                String methodUrl = getMethodUrl(m);
                permission.setUrl(classUrl+methodUrl);
//                System.out.println(permission);
                permissionService.insert(permission);

            }

        }
//        System.out.println(packages);
    }

    public String getMethodUrl(Method method){
        String methodUrl = "";
        // 取枚举中定义的所有的请求类型
        RequestTypeEnum[] requestTypeEnums = RequestTypeEnum.values();
        for (RequestTypeEnum typeEnum : requestTypeEnums) {
            // 根据枚举中的不同的请求类型的class获取该类型对应的注解对象
            Annotation annotation = method.getAnnotation(typeEnum.getRequestType());
            // 如果该请求类型注解不存在就跳过
            if(Objects.isNull(annotation)){
                continue;
            }
            try {
                // 如果该请求类型注解存在,就获取它里面的value方法
                Class<? extends Annotation> aClass = annotation.annotationType();
                // aClass 是 对应的 请求类型的  字节码对象
                Method annotationMethod = aClass.getMethod("value");
                // 通过invoke调用该对象的value方法,获取结果
                // 反射执行 方法 方法名.invoke(对象)

                String[] value = (String[]) annotationMethod.invoke(annotation);
                // 如果value不为空且长度大于0,就赋值给methodUrl,并跳出循环
                if(value != null && value.length > 0){
                    methodUrl = value[0];
                    break;
                }
            }catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }
        return methodUrl;

    }


}
